//
//  ShowDetail.m
//  penzer
//
//  Created by caiyc on 2018/10/18.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "ShowDetail.h"

@implementation ShowDetail

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(void)setModel:(LiveModel *)model{
    _model = model;
    if(model.count)
    [self countDownNotification];
}
-(void)setDic:(NSDictionary *)dic{
    NSDictionary *goods = dic[@"goods_list"][0];
    NSDictionary *hostInfo = dic[@"anchor_list"][0];
    NSURL *goodUrl = [NSURL URLWithString:goods[@"thumb"]];
    [self.images sd_setImageWithURL:goodUrl];
    self.title.text = goods[@"title"];
    NSString *oldStr =  [NSString stringWithFormat:@"市场价%@",goods[@"price"]];
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle],NSForegroundColorAttributeName:[UIColor whiteColor]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:[oldStr substringWithRange: NSMakeRange(0, oldStr.length)] attributes:attribtDic];
    self.old_Price.attributedText = attribtStr;
    // self.oldPrice.text = [NSString stringWithFormat:@"市场价%@",goods[@"price"]];
    self.storename.text = dic[@"title"];
    self.room.text = [NSString stringWithFormat:@"房间：%@",dic[@"room_number"]];
    NSURL *iconUrl = [NSURL URLWithString:hostInfo[@"avatar"]];
    [self.hostIcon sd_setImageWithURL:iconUrl placeholderImage:[UIImage imageNamed:@"logo"]];
    self.hostNick.text = hostInfo[@"nickname"];
    NSURL *stroeIcon = [NSURL URLWithString:dic[@"logo"]];
    [self.storeIcon sd_setImageWithURL:stroeIcon placeholderImage:[UIImage imageNamed:@"logo"]];
    self.price.text = [NSString stringWithFormat:@"¥%@元",goods[@"buy_price"]];
    self.live_type.text = dic[@"live_type_cn"];
    NSString *timeStr = [XTool timeFrommartTotimeintral:dic[@"start_time"]];
    self.live_time.text = timeStr;
    self.nums.text=  [NSString stringWithFormat:@"%@/%@",dic[@"total_member"],dic[@"member_max"]];
    NSArray *goodss =  dic[@"goods_list"];
    NSInteger cousn = [goodss count];
    self.goods_Num.text = [NSString stringWithFormat:@"%lu件商品",cousn];
    
//    countDwonTime = [dic[@"end_time"] integerValue] -  [nowtime integerValue];
//    if(countDwonTime<0){
//        //        self.live_Status.hidden = 0;
//        //        self.live_Status.text = @"直播中";
//    }
//    endtime =  [dic[@"end_time"] integerValue] -  [nowtime integerValue];
//    if(endtime<0){
//        //        self.live_Status.hidden = 0;
//        //        self.live_Status.text = @"直播已结束";
//    }
}
//- (IBAction)enter:(UIButton *)sender {
//    self.enrts(self.tag);
//  
//}
//- (IBAction)share:(UIButton *)sender {
//    self.share(self.tag);
//}
- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    self = [[[NSBundle mainBundle] loadNibNamed:@"ShowDetail" owner:self options:nil] lastObject];
    if (self) {
        self.frame = frame;
        self.enter_Btn.layer.cornerRadius=20;
        self.sahre_Btn.layer.cornerRadius = 20;
        self.enter_Btn.layer.masksToBounds = 1;
        self.sahre_Btn.layer.masksToBounds = 1;
        self.time_botom_View1.layer.cornerRadius = 15;
        self.time_botom_View1.layer.masksToBounds = 1;
        self.time_bottom_View2.layer.cornerRadius = 15;
        self.time_bottom_View2.layer.masksToBounds = 1;
        self.sale_type.layer.cornerRadius = 11;
        self.sale_type.layer.masksToBounds = 1;
        self.hostIcon.layer.cornerRadius = 15;
        self.hostIcon.layer.masksToBounds = 1;
    }
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(countDownNotification)
                                                 name:TimeCellNotification
                                               object:nil];
    return self;
}
- (void)countDownNotification {
    
    LiveModel *model = self.model;
    NSInteger timeInterval;
    if(model.count<=0){
        self.liveStatuss_Lab.hidden = 0;
        self.liveStatuss_Lab.text = @"直播已结束";
        self.time_botom_View1.hidden = 1;
        self.time_bottom_View2.hidden = 1;
        self.mao.hidden = 1;
        self.live_Status.hidden = 1;
        return;
    }
    if (model.countDownSource) {
        //  timeInterval = [kCountDownManager timeIntervalWithIdentifier:model.countDownSource];
    }else {
        //   timeInterval = kCountDownManager.timeInterval;
    }
    NSInteger countDown;
    NSLog(@"首页倒计时时间=%ld,=%ld",(long)model.startcount,(long)model.count);
    //第一次数据进来要做相应处理
    if(model.startcount>0){
        countDown =  model.startcount ;
        self.live_Status.text = @"后开始";
    }else{
        countDown = model.count ;
        self.live_Status.text = @"后结束";
    }
    NSLog(@"倒计时j时间===%ld",(long)countDown);
    [self getMMSSFromSS:countDown];
}
-(NSString *)getMMSSFromSS:(NSInteger )totalTime{
    NSLog(@"...计算时间长度==%ld",totalTime);
    NSInteger seconds = totalTime;
    NSInteger hour = seconds/3600;
    if(hour>0){
        self.time_botom_View1.hidden = 1;
        self.time_bottom_View2.hidden = 1;
        self.mao.hidden = 1;
        self.live_Status.hidden = 1;
        if(_model.startcount>0){
           // self.liveStatuss_Lab.text = [NSString stringWithFormat:@"直播将于%ld小时后开始",(long)hour];
            self.liveStatuss_Lab.text =@"直播未开始";
        }else{
            self.liveStatuss_Lab.text = @"正在直播中";
        }
        
    }else{
        self.liveStatuss_Lab.hidden = 1;
    }
    NSString *str_hour = [NSString stringWithFormat:@"%ld",seconds/3600];
    //format of minute
    NSInteger minute = (seconds%3600)/60;
    NSString *str_minute = [NSString stringWithFormat:@"%ld",(seconds%3600)/60];
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%ld",seconds%60];
    //format of minute
    // NSString *str_minute = [NSString stringWithFormat:@"%ld",seconds/60];
    if(str_minute.length>1){
        self.h_time.text = [str_minute substringWithRange:NSMakeRange(0, 1)];
        self.s_time.text =  [str_minute substringWithRange:NSMakeRange(1, 1)];
    }else{
        self.s_time.text = str_minute;
    }
    if(str_second.length>1){
        self.s_time1.text = [str_second substringWithRange:NSMakeRange(0, 1)];
        self.s_time2.text =  [str_second substringWithRange:NSMakeRange(1, 1)];
    }else{
        self.s_time1.text = @"0";
        self.s_time2.text = str_second;
    }
    NSString *format_time = [NSString stringWithFormat:@"%@分钟%@秒",str_minute,str_second];
    return format_time;
}

@end
